package com.haiyou.common.valid.strategy;

import com.haiyou.common.exception.BusinessException;
import com.haiyou.common.valid.annotation.NotNullValid;
import org.springframework.util.StringUtils;

/**
 * 参数是否为空验证策略
 **/
public class NotNullValidStrategy implements ValidStrategy {

    @Override
    public void doOperation(Object target, Object targetAnnotations) {
        NotNullValid notNull = (NotNullValid) targetAnnotations;
        if (target instanceof Object[]) {
            if (((Object[]) target).length == 0) {
                throw new BusinessException(notNull.message());
            }
        } else if (StringUtils.isEmpty(target)) {
            throw new BusinessException(notNull.message());
        }
    }
}